﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OLMS.DAL;

namespace OLMS.BLL
{
    public class RoleBLL
    {
        //获取全部角色
        public static List<Roles> GetRolesList()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Roles.ToList();
            }
        }
        //根据编号获取角色信息
        public static Roles GetRolesById(int roleId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Roles.SingleOrDefault(g => g.RoleId == roleId);
            }
        }

        //添加和编辑角色
        public static bool AddOrEditRole(Roles roles)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                if (roles.RoleId == 0)
                {
                    db.Roles.Add(roles);
                }
                else
                {
                    db.Roles.Attach(roles);
                }
                return db.SaveChanges() != -1 ? true : false;
            }
        }
        //删除角色
        public static bool RemoveRole(int roleId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Roles roles = db.Roles.SingleOrDefault(a => a.RoleId == roleId);
                if (roles != null)
                {
                    db.Roles.Remove(roles);
                    return db.SaveChanges() != -1 ? true : false;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}